package com.csf.service.impl;

import com.csf.config.likeKeyConfig;
import com.csf.service.likeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

@Service
public class likeServiceImpl implements likeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void addLike(int entityType, int entityId, int userId,int userLikeId) {
//        String likeKey = likeKeyConfig.getKeyName(entityType, entityId);
////        判断当前已经点赞了
//        Boolean isMem = redisTemplate.opsForSet().isMember(likeKey,userId);
//        if (isMem){
////        再次点击就取消点赞
//            redisTemplate.opsForSet().remove(likeKey,userId);
//        }else {
//            redisTemplate.opsForSet().add(likeKey,userId);
//        }


//        当点赞时，我们需要对当前帖子点赞数加一，同时也需要对该帖子的作者点赞总数加一，这时候就需要用到事务
          redisTemplate.execute(new SessionCallback() {
              @Override
              public Object execute(RedisOperations redisOperations) throws DataAccessException {
                  String likeKey = likeKeyConfig.getKeyName(entityType, entityId);
                  String userdisCuss = likeKeyConfig.getLikeUserName(userLikeId);
                  //        判断当前已经点赞了
                  Boolean isMem = redisTemplate.opsForSet().isMember(likeKey,userId);
//                  启用线程
                  redisOperations.multi();
                  if (isMem){
                      //        再次点击就取消点赞
                      redisTemplate.opsForSet().remove(likeKey,userId);
                      redisTemplate.opsForValue().decrement(userdisCuss);
                  }else {
                      redisTemplate.opsForSet().add(likeKey,userId);
//                      发表帖子或评论的人的总赞数
                      redisTemplate.opsForValue().increment(userdisCuss);
                  }
//                  提交事务
                  return redisOperations.exec();
              }
          });
    }

    @Override
    public int likeCount(int entityType, int entityId) {
        String likeKey = likeKeyConfig.getKeyName(entityType, entityId);
        return  Integer.parseInt(String.valueOf(redisTemplate.opsForSet().size(likeKey)));
    }

    @Override
    public int likeStatus(int userId, int entityType, int entityId) {
        String likeKey = likeKeyConfig.getKeyName(entityType, entityId);
        Boolean member = redisTemplate.opsForSet().isMember(likeKey, userId);
        int isMembers = member ? 1:0;
        return isMembers;
    }

    @Override
    public int likeUserCount(int userLikeId) {
        String userdisCuss = likeKeyConfig.getLikeUserName(userLikeId);
        Integer countLikes = (Integer) redisTemplate.opsForValue().get(userdisCuss);
//        判断该用户的点赞数是否为空
        return countLikes==null?0:countLikes.intValue();
    }


}
